//
//  B_MyWalletViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/18.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_MyWalletViewController.h"
#import "B_ReleaseFooterView.h"
#import "B_MyWalletTVC.h"
#import "B_MyWalletHeaderView.h"
#import "B_MyBankListTVC.h"

#import "B_AddBankViewController.h"
#import "B_BalanceDetailListVC.h"
#import "B_MyWalletModel.h"

@interface B_MyWalletViewController ()

@property (nonatomic, strong) B_MyWalletModel * walletModel;

@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_ReleaseFooterView * addBankFooter;

@end

@implementation B_MyWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的钱包"];
    self.view = self.mainView;
    [self getData];
}

- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userBalance/selectUserBalance/%@", getUserId()] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.walletModel = [B_MyWalletModel mj_objectWithKeyValues:data[@"data"]];
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常, 请稍后重试!"];
    }];
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView addSubview:self.addBankFooter];
        [self.addBankFooter mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(80));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (80 + 24) : 80;
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_MyWalletTVC" bundle:nil] forCellReuseIdentifier:@"B_MyWalletTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_MyBankListTVC" bundle:nil] forCellReuseIdentifier:@"B_MyBankListTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 1;
            } else {
                if (ISNULL(weSelf.walletModel)) {
                    return 0;
                }
                return 1;
            }
        };
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0.1f;
            } else {
                return 50;
            }
        };
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return nil;
            } else {
                B_MyWalletHeaderView * header = [B_MyWalletHeaderView initB_MyWalletHeaderView];
                
                return header;
            }
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                B_MyWalletTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MyWalletTVC" forIndexPath:indexPath];
                cell.B_MoneyLabel.text = safeValue1(weSelf.walletModel.balance);
                cell.B_rulerLabel.text = @"账户余额每月15日发放到您绑定的银行卡上";
                cell.watchBalanceAction = ^{
                    B_BalanceDetailListVC * balance = [[B_BalanceDetailListVC alloc] init];
                    
                    [weSelf pushNextViewController:balance];
                };
                return cell;
            } else {
                B_MyBankListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MyBankListTVC" forIndexPath:indexPath];
                cell.B_bankName.text = weSelf.walletModel.bankType;
                cell.B_bankNumber.text = weSelf.walletModel.bandCard;
                return cell;
            }
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 210;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
    }
    return _mainView;
}

- (B_ReleaseFooterView *)addBankFooter {
    if (!_addBankFooter) {
        WeakSelf(weSlef, self);
        _addBankFooter = [B_ReleaseFooterView initB_ReleaseFooterView];
        _addBankFooter.B_SureButton.backgroundColor = [UIColor color_content_background_color];
        [_addBankFooter.B_SureButton setTitle:@"添加银行卡" forState:UIControlStateNormal];
        _addBankFooter.sureActionBlock = ^{
            //TODO:执行退出操作
            B_AddBankViewController * addBank = [[B_AddBankViewController alloc] init];
            [weSlef pushNextViewController:addBank];
        };
    }
    return _addBankFooter;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
